package cn.t.persistence.dao;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.strategy.SaStrategy;
import cn.dev33.satoken.util.SaFoxUtil;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Component
public class JSaTokenDao implements SaTokenDao {
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String TIME_PATTERN = "HH:mm:ss";
    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_PATTERN);
    public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern(TIME_PATTERN);
    public RedissonClient redissonClient;
    public boolean isInit;

    public JSaTokenDao() {
    }

    @Autowired
    public void init(RedissonClient redissonClient) {
        if (!this.isInit) {
            try {

                JavaTimeModule timeModule = new JavaTimeModule();
                timeModule.addSerializer(new LocalDateTimeSerializer(DATE_TIME_FORMATTER));
                timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DATE_TIME_FORMATTER));
                timeModule.addSerializer(new LocalDateSerializer(DATE_FORMATTER));
                timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DATE_FORMATTER));
                timeModule.addSerializer(new LocalTimeSerializer(TIME_FORMATTER));
                timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(TIME_FORMATTER));
                SaStrategy.instance.createSession = JSaSession::new;
            } catch (Exception var5) {
                System.err.println(var5.getMessage());
            }

            this.redissonClient = redissonClient;
            this.isInit = true;
        }
    }

    @Override
    public String get(String key) {
        RBucket<String> rBucket = this.redissonClient.getBucket(key);
        return (String)rBucket.get();
    }
    @Override
    public void set(String key, String value, long timeout) {
        log.info("set,key 1.{} value.{} timeout.{}", key, value,timeout);
        RBucket<String> bucket = this.redissonClient.getBucket(key);
        bucket.set(value, timeout, TimeUnit.SECONDS);
    }
    @Override
    public void update(String key, String value) {
        log.info("update,key.{} value.{}", key, value);
        long expire = this.getTimeout(key);
        if (expire != -2L) {
            this.set(key, value, expire);
        }
    }
    @Override
    public void delete(String key) {
        log.info("delete,key.{}", key);
        this.redissonClient.getBucket(key).delete();
    }
    @Override
    public long getTimeout(String key) {
        RBucket<String> rBucket = this.redissonClient.getBucket(key);
        long timeout = rBucket.remainTimeToLive();
        return timeout < 0L ? timeout : timeout / 1000L;
    }
    @Override
    public void updateTimeout(String key, long timeout) {
        if (timeout == -1L) {
            long expire = this.getTimeout(key);
            if (expire != -1L) {
                this.set(key, this.get(key), timeout);
            }

        } else {
            RBucket<String> rBucket = this.redissonClient.getBucket(key);
            rBucket.expire( Instant.ofEpochSecond(timeout));
        }
    }
    @Override
    public Object getObject(String key) {
        RBucket<Object> rBucket = this.redissonClient.getBucket(key);
        return rBucket.get();
    }
    @Override
    public void setObject(String key, Object object, long timeout) {
        log.info("setObject 1,key.{} value.{} timeout.{}", key, object,timeout);
        RBucket<Object> bucket = this.redissonClient.getBucket(key);
        bucket.set(object, timeout, TimeUnit.SECONDS);
    }
    @Override
    public void updateObject(String key, Object object) {
        long expire = this.getObjectTimeout(key);
        if (expire != -2L) {
            this.setObject(key, object, expire);
        }
    }
    @Override
    public void deleteObject(String key) {
        this.redissonClient.getBucket(key).delete();
    }
    @Override
    public long getObjectTimeout(String key) {
        RBucket<String> rBucket = this.redissonClient.getBucket(key);
        long timeout = rBucket.remainTimeToLive();
        return timeout < 0L ? timeout : timeout / 1000L;
    }
    @Override
    public void updateObjectTimeout(String key, long timeout) {
        if (timeout == -1L) {
            long expire = this.getObjectTimeout(key);
            if (expire != -1L) {
                this.setObject(key, this.getObject(key), timeout);
            }

        } else {
            RBucket<Object> rBucket = this.redissonClient.getBucket(key);
            rBucket.expire(Instant.ofEpochSecond(timeout));
        }
    }
    @Override
    public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
        Stream<String> stream = this.redissonClient.getKeys().getKeysStreamByPattern(prefix + "*" + keyword + "*");
        List<String> list = stream.collect(Collectors.toList());
        return SaFoxUtil.searchList(list, start, size, sortType);
    }
}

